package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import qlsl.androiddesign.activity.subactivity.CopyActivity;
import qlsl.androiddesign.activity.subactivity.ExChangeOrderActivity;
import qlsl.androiddesign.activity.subactivity.ExChangeOrderActivity;
import qlsl.androiddesign.activity.subactivity.PostProvisionActivity;
import qlsl.androiddesign.activity.subactivity.PostingShoppingDetailActivity;
import qlsl.androiddesign.activity.subactivity.RecorderActivity;
import qlsl.androiddesign.activity.subactivity.RefundActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingDetailActivity;
import qlsl.androiddesign.activity.subactivity.SignActivity;
import qlsl.androiddesign.adapter.subadapter.PostingAdapter;
import qlsl.androiddesign.adapter.subadapter.PostingListAdapter;
import qlsl.androiddesign.entity.commonentity.FragmentArgs;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.GoodsList;
import qlsl.androiddesign.entity.otherentity.Posting;
import qlsl.androiddesign.entity.otherentity.ShoppingDetail;
import qlsl.androiddesign.http.service.subservice.SignService;
import qlsl.androiddesign.method.UserMethod;
import qlsl.androiddesign.util.commonutil.ImageUtils;
import qlsl.androiddesign.util.runfeng.AbJsonUtil;
import qlsl.androiddesign.util.runfeng.ToActivityUtil;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 签到页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class SignView extends FunctionView<SignActivity> {

	private Pager pager;

	private ListView listView;

	private List<ShoppingDetail> list = new ArrayList<ShoppingDetail>();

	public SignView(SignActivity activity) {
		super(activity);
		setContentView(R.layout.activity_sign);
	}

	protected void initView(View view) {
		setTitle("签到");
		TextView aa = (TextView) findViewById(R.id.color_name);
		listView = findViewById(R.id.listView);

		TextView tv_name = findViewById(R.id.tv_name);
		ImageView iv_photo = findViewById(R.id.iv_icon);
		tv_name.setText(UserMethod.getUser().getPoints() + "挖币");
		ImageUtils.circle(activity, UserMethod.getUser().getPhoto(), iv_photo);

		aa.setText("挖币兑换区");
		setRightButtonText("兑换记录");
		showRightButton();
	}

	protected void initData() {
		SignService.queryPointsGoodsList(1, this, activity);

	}

	protected void initListener() {

	}

	public <T> void showData(T... t) {
		if (t[0] instanceof HashMap) {
			HashMap<String, Object> map = (HashMap<String, Object>) t[0];
			List<ShoppingDetail> list_result = (List<ShoppingDetail>) map
					.get("list");
			pager = (Pager) map.get("pager");
			if (pager.getPageNo() == 1) {
				resetList(list_result);
			} else {
				list.addAll(list_result);
			}
			notifyDataSetChanged();
		}

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.comment:// 点击查看快挖说明
			FragmentArgs args = new FragmentArgs();
			args.add("name", "挖币说明");
			args.add("url", "file:///android_asset/other/explain.html");
			ToActivityUtil.toNextActivity(activity, RefundActivity.class,
					"data", args);
			break;
		case R.id.assessment_again:// 点击签到按钮
			doClickSign();
			break;
		case R.id.grid_item:
			doClickGridItem(view);
			break;
		case R.id.reply_send:
			doClickSend(view);
			break;
		case R.id.btn_right:
			doClickBtnRight(view);
			break;
		}

	}

	private void doClickBtnRight(View view) {
		startActivity(RecorderActivity.class);
	}

	private void doClickSend(View view) {
		int index = listView.getPositionForView(view);
		ShoppingDetail item = list.get(index);
		FragmentArgs fragmentArgs2 = new FragmentArgs();
		List<GoodsList> aa = new ArrayList();
		GoodsList g = new GoodsList();
		g.setGoodsinfoid("" + item.getGoodsid());
		g.setNums("1");// 商品的数量
		aa.add(g);
		// double pricev = Double.valueOf();

		fragmentArgs2.add("goodslist", AbJsonUtil.toJson(aa));// 订单信息
		fragmentArgs2.add("totalprice", item.getPoints());// 商品总积分

		fragmentArgs2.add("goodsid", item.getGoodsid());// 商品id

		// ToActivityUtil.toNextActivity(activity, ExChangeOrderActivity.class,
		// "goumainBundle", fragmentArgs2);
		ToActivityUtil.toNextActivity(activity, ExChangeOrderActivity.class,
				"goumainBundle", fragmentArgs2);

	}

	private void doClickGridItem(View view) {
		int position = listView.getPositionForView(view);
		ShoppingDetail item = (ShoppingDetail) listView.getAdapter().getItem(
				position);
		if (item != null) {
			startActivity(item, PostingShoppingDetailActivity.class);
		}
	}

	private void doClickSign() {
		SignService.createSign(this, activity);

	}

	private void resetList(List<ShoppingDetail> list_result) {
		list.clear();
		list.addAll(list_result);
	}

	private void notifyDataSetChanged() {
		BaseAdapter adapter = (BaseAdapter) listView.getAdapter();
		if (adapter == null) {
			adapter = new PostingListAdapter(activity, list);
			listView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
	}

}
